﻿package mortal.game.view.common.guide.stepOperater
{
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.*;
    import mortal.common.net.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.view.common.guide.data.*;
    import mortal.game.view.guide.*;
    import mortal.mvc.core.*;

    public class AutoGuideLayerSteper extends AutoGuideStepBase implements IAutoGuideSteper
    {
        private var _opObj:InteractiveObject;
        private var rect:Rectangle;
        private var _winx:Window;

        public function AutoGuideLayerSteper()
        {
            return;
        }// end function

        public function guide(param1:AutoGuideData, param2:Window = null) : void
        {
            var _loc_5:Array = null;
            var _loc_6:String = null;
            var _loc_7:String = null;
            var _loc_8:DisplayObject = null;
            var _loc_9:Array = null;
            if (param1 == null)
            {
                return;
            }
            if (_working)
            {
                this.stop();
            }
            _data = param1;
            this._winx = null;
            if (param1.shortCutPos >= 0 && param1.shortCutPos <= 20)
            {
                Dispatcher.addEventListener(EventName.ShortcutBarKeyDown, this.keyDownHandler);
                param1.clickChildName = "shortCutBar_Pos" + param1.shortCutPos.toString();
            }
            var _loc_3:* = LayerManager[param1.guideTypeValue as String] as DisplayObjectContainer;
            var _loc_4:* = _loc_3;
            this.rect = null;
            if (param1.clickChildName.indexOf(";") >= 0)
            {
                this.rect = new Rectangle();
                _loc_5 = String(param1.clickChildName).split(";");
                this.rect.x = int(_loc_5[0]);
                this.rect.y = int(_loc_5[1]);
                this.rect.width = int(_loc_5[2]);
                this.rect.height = int(_loc_5[3]);
                if (_loc_5.length >= 5)
                {
                    if (_loc_5[4] == 1)
                    {
                        this.rect.x = Global.stage.stageWidth - int(_loc_5[0]);
                        this.rect.y = int(_loc_5[1]);
                    }
                    else if (_loc_5[4] == 2)
                    {
                        this.rect.x = Global.stage.stageWidth - int(_loc_5[0]);
                        this.rect.y = Global.stage.stageHeight - int(_loc_5[1]);
                    }
                    else if (_loc_5[4] == 3)
                    {
                        this.rect.x = int(_loc_5[0]);
                        this.rect.y = Global.stage.stageHeight - int(_loc_5[1]);
                    }
                }
                if (param1.needMask)
                {
                    showMaskByRect(_loc_3, this.rect);
                }
                if (param1.needArrow)
                {
                    showArrowByRect(_loc_3, param1, this.rect);
                }
                if (param1.needLightMask)
                {
                    showLightMaskByRect(_loc_3, this.rect);
                }
                if (param1.needCircleEffect)
                {
                    showCircleEffectByRect(_loc_3, this.rect);
                }
                if (param1.clickType == AutoGuideClickType.Click)
                {
                    _loc_3.addEventListener(MouseEvent.CLICK, this.clickWinChildHandler, false, 0, true);
                }
                else
                {
                    _loc_3.addEventListener(MouseEvent.DOUBLE_CLICK, this.clickWinChildHandler, false, 0, true);
                }
                this._opObj = _loc_3;
            }
            else
            {
                _loc_6 = param1.clickChildName;
                _loc_7 = "";
                if (_loc_6.indexOf(",") >= 0)
                {
                    _loc_9 = _loc_6.split(",");
                    _loc_7 = _loc_9[0];
                    _loc_6 = _loc_9[1];
                }
                _loc_8 = DisplayUtil.getChildByPropety(_loc_3, "name", _loc_6);
                if (_loc_7 != "")
                {
                    this._winx = DisplayUtil.getChildByPropety(_loc_3, "winTitleName", _loc_7) as Window;
                    if (this._winx == null)
                    {
                        this.stop();
                        return;
                    }
                    _loc_4 = this._winx.contentContainer;
                }
                if (_loc_8 == null && this._winx != null)
                {
                    _loc_8 = DisplayUtil.getChildByPropety(this._winx.contentTopOf3DSprite, "name", _loc_6);
                    _loc_4 = this._winx.contentTopOf3DSprite;
                }
                if (this._winx != null)
                {
                    _loc_3 = this._winx.highestContentContainer;
                }
                if (_loc_8 == null)
                {
                    Log.debug("Layer自动指引，Layer：" + _loc_3 + "中找不到name为：" + param1.guideTypeValue + " 的儿子儿儿子， 指引中断");
                    Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideStepEnd, param1));
                    return;
                }
                if (param1.needMask)
                {
                    showMask(_loc_3, _loc_8);
                }
                if (param1.needArrow)
                {
                    showArrowByChild(_loc_3, param1, _loc_8);
                }
                if (param1.needLightMask)
                {
                    showLightMask(_loc_3, _loc_8, 16, 16);
                }
                if (param1.needCircleEffect)
                {
                    showCircleEffect(_loc_3, _loc_8);
                }
                this._opObj = _loc_8 as InteractiveObject;
                if (param1.clickType == AutoGuideClickType.Click)
                {
                    this._opObj.addEventListener(MouseEvent.CLICK, this.clickWinChildHandler, false, 0);
                }
                else
                {
                    this._opObj.addEventListener(MouseEvent.DOUBLE_CLICK, this.clickWinChildHandler, false, 0);
                }
            }
            super.setData(param1, _loc_3, _loc_8, _loc_4);
            if (this._winx != null)
            {
                this._winx.addEventListener(WindowEvent.CLOSE, this.winCloseHandler);
            }
            return;
        }// end function

        override protected function guideTimerOut() : void
        {
            if (_data != null)
            {
                if (_data.subType == AutoGuideActivedType.close && LayerManager.isHasFullSceenOpen())
                {
                    PopupManager.closeAllFullView();
                }
            }
            if (this._opObj != null)
            {
                this._opObj.removeEventListener(MouseEvent.CLICK, this.clickWinChildHandler);
                this._opObj.removeEventListener(MouseEvent.DOUBLE_CLICK, this.clickWinChildHandler);
                this._opObj = null;
            }
            Dispatcher.removeEventListener(EventName.ShortcutBarKeyDown, this.keyDownHandler);
            if (this._winx != null)
            {
                this._winx.removeEventListener(WindowEvent.CLOSE, this.winCloseHandler);
                this._winx.hide();
            }
            super.guideTimerOut();
            this._winx = null;
            return;
        }// end function

        private function winCloseHandler(event:WindowEvent) : void
        {
            var nextFrame:Function;
            var evt:* = event;
            nextFrame = function () : void
            {
                if (_winx == null)
                {
                    return;
                }
                stop();
                if (GuideController.shouldResumeToTaskAfterGuide() && _data.filterName != "NoMainEndType")
                {
                    GameController.guide.resumeToTask();
                }
                return;
            }// end function
            ;
            if (this._winx == null)
            {
                return;
            }
            CallLater.addCallBack(nextFrame);
            return;
        }// end function

        private function keyDownHandler(event:DataEvent) : void
        {
            var _loc_2:* = int(event.data);
            if (_loc_2 == _data.shortCutPos)
            {
                this.stop(_data);
            }
            return;
        }// end function

        private function clickWinChildHandler(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            if (this.rect != null && this._opObj != null)
            {
                _loc_2 = this._opObj.mouseX;
                _loc_3 = this._opObj.mouseY;
                if (_loc_2 < this.rect.x || _loc_3 < this.rect.y)
                {
                    return;
                }
                if (_loc_2 > this.rect.x + this.rect.width)
                {
                    return;
                }
                if (_loc_3 > this.rect.y + this.rect.height)
                {
                    return;
                }
            }
            this.stop(_data);
            return;
        }// end function

        override public function stop(param1:AutoGuideData = null) : void
        {
            super.stop(param1);
            if (this._winx != null)
            {
                this._winx.removeEventListener(WindowEvent.CLOSE, this.winCloseHandler);
            }
            if (this._opObj != null)
            {
                this._opObj.removeEventListener(MouseEvent.CLICK, this.clickWinChildHandler);
                this._opObj.removeEventListener(MouseEvent.DOUBLE_CLICK, this.clickWinChildHandler);
                this._opObj = null;
            }
            Dispatcher.removeEventListener(EventName.ShortcutBarKeyDown, this.keyDownHandler);
            if (param1 != null)
            {
                if (param1.subType == AutoGuideActivedType.close && LayerManager.isHasFullSceenOpen())
                {
                    PopupManager.closeAllFullView();
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideStepEnd, param1));
            }
            return;
        }// end function

    }
}
